package com.xrz.sxm.aj.base;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.LinkedList;
import java.util.List;

/**
 * BaseAdapter的实现类
 *
 * @author 辛汝州
 *
 * @param <T>
 */
public abstract class AdapterBase<T> extends BaseAdapter {

	private final List<T> mList = new LinkedList<T>();
	private OnReachBottomListener lis;

	/**
	 * 获取当前List
	 *
	 * @return
	 */
	public List<T> getList() {
		return mList;
	}
	/**
	 * 删除一个
	 * @param pos
	 */
	public void removeByPos(int pos){
		mList.remove(pos);
		notifyDataSetChanged();
	}

	/**
	 * 添加一个List
	 *
	 * @param list
	 */
	public void appendToList(List<T> list) {
		if (list == null) {
			return;
		}
		mList.addAll(list);
		notifyDataSetChanged();
	}

	public void appToList(T t) {
		mList.add(t);
		notifyDataSetChanged();
	}

	public void appendToTopList(T t) {
		mList.add(0, t);
		notifyDataSetChanged();
	}

	/**
	 * 添加一个List到第一个
	 *
	 * @param list
	 */
	public void appendToTopList(List<T> list) {
		if (list == null) {
			return;
		}
		mList.addAll(0, list);
		notifyDataSetChanged();
	}

	/**
	 * 清除List
	 */
	public void clear() {
		mList.clear();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if (position > mList.size() - 1) {
			return null;
		}
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (position == getCount() - 1) {
			if (lis != null)
				lis.onReachBottom();
		}
		return getExView(position, convertView, parent);
	}

	protected abstract View getExView(int position, View convertView, ViewGroup parent);

	public void setOnReachBottomListener(OnReachBottomListener lis) {
		this.lis = lis;
	}
}
